//
//  HEApplyPlusViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/19.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEApplyPlusViewController.h"
#import "HEPlusTextViewCell.h"
#import "HEColorNoticeCell.h"
#import "HEDocOrderHeadCell.h"
#import "HEReportSelectCell.h"

@interface HEApplyPlusViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) GDFormModel *formModel;
@property (nonatomic, strong) NSMutableArray *sections;
@property (nonatomic, strong) UIButton *bottomView;

 
@property (nonatomic, assign) CGFloat topHeight;

@end

@implementation HEApplyPlusViewController

- (GDFormModel *)formModel {
    if (!_formModel) {
        _formModel = [[GDFormModel alloc] init];
        _formModel.proxyVC = self;
    }
    return _formModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"申请加号"; 

    [self initUI];
    [self initCell];
    [self initTabBarView];
}

- (void)initUI {
   CGFloat tabbarHeight = 49 + KSafeTabbarHeight;
    CGFloat topEsoff = _topHeight;
   _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight + topEsoff, KScreenWidth, KScreenHeight- ZXNavBarHeight - tabbarHeight - topEsoff) style:UITableViewStyleGrouped];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
    _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 20)];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    
   _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
   _tableView.scrollsToTop = YES;
   _tableView.tableFooterView = [[UIView alloc] init];
   
   self.tableView.backgroundColor = KWhiteColor;
   self.tableView.delegate = self;
   self.tableView.dataSource = self;
   [self.view addSubview:self.tableView];
}

- (void)initCell {
   // 总段
   _sections = [NSMutableArray array];
   NSMutableArray *classArr = [NSMutableArray array];
    

    self.formModel.doc_details = _userModel;
    self.formModel.ap_phone = [UserManager sharedUserManager].curUserInfo.username;


   // 就诊人
    GDFiterCellModel *userModel = [GDFiterCellModel new];
    userModel.cellKlass = HEDocOrderHeadCell.class;
    
    GDFiterCellModel *phoneModel = [GDFiterCellModel new];
    phoneModel.cellKlass = HEPlusTextViewCell.class;
    phoneModel.leftTitle = @"电话号码：";
    phoneModel.placeHolder = @"请输入电话号码";
    phoneModel.tableKey = @"ap_phone";
    phoneModel.cellType = GDFiterCellTypeSelect_TEXTView;
    phoneModel.isHasMustSigns = YES;

     GDFiterCellModel *cardModel = [GDFiterCellModel new];
    cardModel.cellKlass = HEPlusTextViewCell.class;
    cardModel.leftTitle = @"就诊卡号：";
    cardModel.placeHolder = @"请输入就诊卡号";
    cardModel.tableKey = @"ap_card";
    cardModel.cellType = GDFiterCellTypeSelect_TEXTView;

    
    GDFiterCellModel *cntModel = [GDFiterCellModel new];
    cntModel.cellKlass = HEPlusTextViewCell.class;
    cntModel.leftTitle = @"主诉内容：";
    cntModel.placeHolder = @"请输入主诉内容";
    cntModel.tableKey = @"ap_conent";
    cntModel.cellType = GDFiterCellTypeSelect_TEXTView;
    cntModel.isHasMustSigns = YES;

    
    GDFiterCellModel *jzsjModel = [GDFiterCellModel new];
    jzsjModel.cellKlass = HEReportSelectCell.class;
    jzsjModel.leftTitle = @"预约时间";
    jzsjModel.placeHolder = @"请选择预约时间";
    jzsjModel.pickerType = GDPickerType_Date;
    jzsjModel.pickerDateType = BRDatePickerModeYMD;
    jzsjModel.tableKey = @"ap_time";
    jzsjModel.cellType = GDFiterCellTypeSelect_TIME;
    jzsjModel.minDate = [NSDate date];
    jzsjModel.maxDate = [NSDate getDate:[NSDate date] daysAhead:3];
    
    
    // 就诊科室
    GDFiterCellModel *noticeModel = [GDFiterCellModel new];
    noticeModel.cellKlass = HEColorNoticeCell.class;
    noticeModel.leftTitle = @"请患者或家属务必留下有效的联系方式用于接收相关短信/电话，医生加号资源有限，加号申请原则上不予退号，请谨慎申请。为保证诊疗质量，医生会根据加号当天的实际就诊/已预约的情况开放加号，但不保证最终有号，还望谅解。";
    
    NSMutableArray *secs1 = @[userModel, phoneModel, cardModel, cntModel, jzsjModel, noticeModel].mutableCopy;
     
    [_sections addObject: secs1];

   // 添加的时候注意顺序，暂未对顺序做处理
   classArr = @[userModel, phoneModel, cardModel, cntModel, jzsjModel, noticeModel].mutableCopy;
   [self.tableView mc_registerCellByFiterModels:classArr];
 
   [_tableView reloadData];
}

- (void)initTabBarView {
    UIButton *bottomView = [UIButton buttonWithType:UIButtonTypeCustom];
    _bottomView = bottomView;
    [self.view addSubview:bottomView];
    bottomView.backgroundColor = CNavBgColor;
    _bottomView.titleLabel.font = DYBlodFont(18);
    [bottomView setTitleColor:KWhiteColor forState:UIControlStateNormal];
    [bottomView addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    
    [bottomView setTitle: @"提交申请" forState:UIControlStateNormal];

    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_offset(49);
        make.bottom.equalTo(self.view).offset(-ZXSafeAreaBottom);
    }];
}

- (void)submit {
    
    if ([DYTools isBlankString:self.formModel.ap_phone]) {
        [Dialog toastBottom:@"请输入电话号码!"];
        return;
    }
    
    if ([DYTools isBlankString:self.formModel.ap_conent]) {
        [Dialog toastBottom:@"请输入主诉内容"];
        return;
    }
    
    if ([DYTools isBlankString:self.formModel.ap_time]) {
        [Dialog toastBottom:@"请选择预约时间"];
        return;
    }
    
    NSMutableDictionary *params = @{}.mutableCopy;
    [params setValue:_formModel.ap_card forKey:@"user_card"];
    [params setValue:_userModel.user_id forKey:@"doctor_id"];
    [params setValue:_formModel.ap_phone forKey:@"user_tel"];
    [params setValue:_userModel.depart_name forKey:@"dept_name"];
    [params setValue:_formModel.ap_conent forKey:@"user_note"];
    [params setValue:_formModel.ap_time forKey:@"next_date"];
    [params setValue:_userModel.hospital_name forKey:@"hospital_name"];

    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:Kapplyplus parameters:params success:^(id  _Nonnull responseObject) {
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        if (response.success) {
            [WOAlert alertShowTitle:@"温馨提示" message:@"申请成功，您可前往消息列表查看申请结果~" cancelButtonTitle:@"确定" otherButtonTitles:nil block:^(NSInteger buttonIndex) {
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }

    } failure:^(NSError * _Nonnull error) {

    }];
 
}
 
  

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return _sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   NSMutableArray *sec = _sections[section];
   return sec.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   NSMutableArray *sec = _sections[indexPath.section];
   GDFiterCellModel *fiterModel = sec[indexPath.row];
   
   UITableViewCell *cell = [_tableView mc_dequeueReusableCellWithClassType:fiterModel.cellKlass];
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
   fiterModel.fiterCellModelBlock((MCTableBaseCell *)cell, fiterModel, self.formModel, indexPath);
   return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  NSMutableArray *sec = _sections[indexPath.section];
   GDFiterCellModel *fiterModel = sec[indexPath.row];

    if (fiterModel.cellType == GDFiterCellTypeSelect_TEXTView) {
        if (fiterModel.cellHeight > 0) {
            return fiterModel.cellHeight;
        }else {
            return 60;
        }
    }
   return [tableView fd_heightForCellWithIdentifier: NSStringFromClass(fiterModel.cellKlass) cacheByIndexPath:indexPath configuration:^(MCTableBaseCell *cell) {
              [cell refreshContent:fiterModel formModel:self.formModel indexPath:indexPath];
   }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   NSMutableArray *sec = _sections[indexPath.section];
    GDFiterCellModel *fiterModel = sec[indexPath.row];
 
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    NSMutableArray *sec = _sections[section];
    GDFiterCellModel *fiterModel = sec.lastObject;
    return CGFLOAT_MIN;

}
 
 

#pragma mark - UITableViewDelegate
  
- (void)changeTabbarView {
 
}



@end
